#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "secret_divide.h"
#include <QTableWidgetItem>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->tableWidget_secret->setRowCount(1);
    ui->tableWidget_secret->setColumnCount(2);
    QStringList labels;
    labels.append("Номер");
    labels.append("Часть секрета");
    ui->tableWidget_secret->setHorizontalHeaderLabels(labels);
    ui->tableWidget_secret->verticalHeader()->hide();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::devide_secret()
{
    int result[100];
    int secret = ui->lineEdit_secret->text().toInt();
    int n = ui->spinBox_n->value();
    int k = ui->spinBox_k->value();
    QString eq;

    s_d.divide(secret,n,k,result,eq);


    ui->label_eq->setText(eq);

    ui->tableWidget_secret->setRowCount(n);
    ui->tableWidget_secret->setColumnCount(2);

    for (int i=0;i<n;i++)
    {
        QTableWidgetItem *item = new QTableWidgetItem("K"+QString::number(i+1));
        ui->tableWidget_secret->setItem(i,0,item);
    }
    for (int i=0;i<n;i++)
    {
        QTableWidgetItem *item = new QTableWidgetItem(QString::number(result[i+2]));
        ui->tableWidget_secret->setItem(i,1,item);
    }

}

void MainWindow::restore_secret()
{
    int x[3] = {3,7,5};
    int y[3] = {2,3,5};


    int pow = ui->spinBox_power->value();
    int p  = ui->lineEdit_P->text().toInt();

    QStringList list = ui->lineEdit_numbers->text().split(' ');
    if (list.size()!=pow+1) return;
    for (int i=0;i<list.size();i++) y[i]=list[i].toInt();

    list = ui->lineEdit_secret_parts->text().split(' ');
    if (list.size()!=pow+1) return;
    for (int i=0;i<list.size();i++) x[i]=list[i].toInt();

    ui->label_secret_restored->setText(QString::number(s_d.restore(pow,p,x,y)));
}
